class Solution(object):
    def numberOfPatterns(self, m, n):
        """
        :type m: int
        :type n: int
        :rtype: int
        """
        self.count = 0
        now_list = []
        already_set = set()
        points = {1, 3, 7, 9}

        def judge(now_list):
            judge_set = set()
            for i in range(len(now_list) - 1):
                if (now_list[i] in points and now_list[i + 1] in points) or ((now_list[i] + now_list[i + 1]) == 10):
                    if (now_list[i] + now_list[i + 1]) // 2 not in judge_set:
                        return False
                judge_set.add(now_list[i])
            return True

        def search(k):
            for num in range(1, 10):
                if num not in already_set:
                    now_list.append(num)
                    already_set.add(num)
                    if m <= k + 1 <= n:
                        if judge(now_list):
                            self.count += 1
                    if k + 1 < n:
                        search(k + 1)
                    now_list.pop()
                    already_set.remove(num)
        search(0)
        return self.count


data = Solution()
m = 9
n = 9
print(data.numberOfPatterns(m, n))
